//
//  QHHCPUpdateProgress.m
//  QHealthForDoctor
//
//  Created by 李志良的mac mini on 2020/4/16.
//  Copyright © 2020 李 志良. All rights reserved.
//热更新进度控件

#import "QHHCPUpdateProgress.h"
static QHHCPUpdateProgress * _sharedProgress = nil;

@interface QHHCPUpdateProgress ()

@property (nonatomic,assign) BOOL isRefusedBtnTaped;

@end


@implementation QHHCPUpdateProgress

+ (QHHCPUpdateProgress *)sharedProgress{
    @synchronized(self)
    {
        if (_sharedProgress == nil)
        {
            _sharedProgress = [[QHHCPUpdateProgress alloc] init];
        }
    }
    return _sharedProgress;
}

-(id)init{
    self = [super init];
    if(self){
        [self setUpUI];
    }
    return self;
}

-(void)setUpUI{
    
    [[AppDelegate sharedAppDelegate].navigationViewController.view  addSubview:self.contentView];
    
    [self.contentView addSubview:self.maskView];
    [self.contentView addSubview:self.topRoketView];
    
    [self.topRoketView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDTH*0.8);
        make.centerY.equalTo(_contentView).offset(-95);
        make.height.mas_equalTo(195);
        make.centerX.mas_equalTo(_contentView);
    }];
    
    UILabel * titleLabel = [[UILabel alloc]init];
    [titleLabel setTextColor:[UIColor whiteColor]];
    [titleLabel setText:@"版本更新"];
    [titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
    [self.topRoketView addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(200);
        make.top.mas_equalTo(_topRoketView.mas_top).offset(50);
        make.left.mas_equalTo(_topRoketView.mas_left).offset(25);
        make.height.mas_equalTo(25);
    }];
    
    //边框
    UIView *borderView = [[UIView alloc] init];
    borderView.layer.cornerRadius = 4;
    borderView.layer.masksToBounds = YES;
    borderView.backgroundColor = [UIColor whiteColor];
    borderView.layer.borderColor = RGBA(123, 164, 204, 1).CGColor;
    borderView.layer.borderWidth = 1;
    [self.topRoketView addSubview:borderView];
    
    self.progressBorderView = borderView;
    
    [borderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_topRoketView.mas_left).offset(25);
        make.right.mas_equalTo(_topRoketView.mas_right).offset(-25);
        make.bottom.mas_equalTo(_topRoketView.mas_bottom).offset(-30);
        make.height.mas_equalTo(8);
    }];
    
    //进度
    UIView *tView = [[UIView alloc] init];
    tView.backgroundColor = RGBA(90, 124, 188, 1);
    tView.layer.cornerRadius = 4;
    tView.layer.masksToBounds = YES;
    self.progressView = tView;
    [self.topRoketView addSubview:tView];
    
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_topRoketView.mas_left).offset(25);
        make.width.mas_equalTo(0);
        make.bottom.mas_equalTo(_topRoketView.mas_bottom).offset(-30);
        make.height.mas_equalTo(8);
    }];
    
    [self.contentView addSubview:self.bottomContentView];
    
    [self.bottomContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDTH*0.76);
        make.top.mas_equalTo(_topRoketView.mas_bottom).offset(-25);
        make.centerX.mas_equalTo(_topRoketView.mas_centerX);
        make.height.mas_equalTo(75);
    }];
    
    [self.contentView addSubview:self.alertLabel];
    [self.alertLabel setText:@"更新中，请稍候"];
    
    [self.alertLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//      make.width.mas_equalTo(SCREEN_WIDTH*0.8);
        make.top.mas_equalTo(_bottomContentView.mas_top);
        make.left.equalTo(_topRoketView.mas_left).offset(6);
        make.right.equalTo(_topRoketView.mas_right).offset(-6);
        make.height.mas_equalTo(30);
    }];
    
    [self.bottomContentView addSubview:self.opraView];
    
    [self.opraView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(_bottomContentView.mas_width);
        make.top.mas_equalTo(_alertLabel.mas_bottom).offset(5);
        make.bottom.mas_equalTo(_bottomContentView.mas_bottom);
        make.left.mas_equalTo(_bottomContentView.mas_left);
    }];
    
    UIView * lineView = [[UIView alloc]init];
    [lineView setBackgroundColor:RGBA(220, 220, 220, 1)];
    [self.opraView addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(_opraView.mas_width);
        make.top.mas_equalTo(_opraView.mas_top);
        make.left.mas_equalTo(_opraView.mas_left);
        make.height.mas_equalTo(1);
    }];
    
    UIView * v_Line = [[UIView alloc]init];
    [v_Line setBackgroundColor:RGBA(220, 220, 220, 1)];
    [self.opraView addSubview:v_Line];
    
    [v_Line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(1);
        make.top.mas_equalTo(_opraView.mas_top);
        make.bottom.mas_equalTo(_opraView.mas_bottom);
        make.centerX.mas_equalTo(_opraView.mas_centerX);
    }];
    
    UIButton * cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton addTarget:self action:@selector(hideProgress) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [self.opraView addSubview:cancelButton];
    
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_opraView.mas_top);
        make.left.mas_equalTo(_opraView.mas_left);
        make.right.mas_equalTo(v_Line.mas_left);
        make.bottom.mas_equalTo(_opraView.mas_bottom);
    }];
    
    _retryButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_retryButton addTarget:self action:@selector(retryButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [_retryButton setTitle:@"重试" forState:UIControlStateNormal];
    [_retryButton setTitleColor:RGBA(90, 124, 188, 1) forState:UIControlStateNormal];
    [self.opraView addSubview:_retryButton];
    
    [_retryButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_opraView.mas_top);
        make.left.mas_equalTo(v_Line.mas_right);
        make.right.mas_equalTo(_opraView.mas_right);
        make.bottom.mas_equalTo(_opraView.mas_bottom);
    }];
}

-(void)show{
    [_contentView setAlpha:1.0];
}
-(void)showErrorMessage{
    [self.alertLabel setTextColor:RGBA(242, 8, 8, 1)];
    [self.alertLabel setText:@"网络错误，下载失败～"];
    [self.alertLabel setHidden:NO];
    [self.opraView setHidden:NO];
}

-(void)showCompleteMessage{
    [self.alertLabel setHidden:NO];
    [self.alertLabel setText:@"更新成功！"];
}


-(void)hideProgress
{
    [self.opraView setAlpha:0.0];
    [self.contentView setAlpha:0.0];
    [self.opraView setHidden: YES];
    [self.alertLabel setHidden:YES];
    [self.progressView setHidden: YES];
    self.isRefusedBtnTaped = YES;
}

-(void)showErrorWithMsg:(NSString *)errorMsg
{
    [self.alertLabel setTextColor:RGBA(242, 8, 8, 1)];
    self.alertLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    [self.alertLabel setText:errorMsg];
    [self.alertLabel setHidden:NO];
    [self.opraView setHidden:NO];
    
}

-(void)showNetWorkStatusMsg:(NSString *)errorMsg
{
    [self.alertLabel setTextColor:[UIColor lightGrayColor]];
    self.alertLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    [self.alertLabel setText:errorMsg];
    [self.alertLabel setHidden:NO];
    [self.opraView setHidden:NO];
}

-(void)retryButtonTapped{
    
    NSLog(@"=========>>>>>%f",self.progressBorderView.frame.size.width);
    [self.alertLabel setText:@""];
    self.alertLabel.hidden = YES;
    [self.opraView setHidden:YES];
    [self.progressView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0.01*self.progressBorderView.frame.size.width);
    }];
    if (self.retryBtnClickBlock) {
        self.retryBtnClickBlock();
    }
}

-(void)setUpdateProgress:(CGFloat)updateProgress{
    if (self.isRefusedBtnTaped) {
        return;
    }
    
    [self.progressView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(updateProgress*self.progressBorderView.frame.size.width);
    }];
}

-(UIView*)contentView{
    if(!_contentView){
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, Main_Screen_Width, Main_Screen_Height)];
        [_contentView setAlpha:0.0];
    }
    return _contentView;
}

-(UIView*)maskView{
    if(!_maskView){
        _maskView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, Main_Screen_Width, Main_Screen_Height)];
        [_maskView setBackgroundColor:RGBA(0, 0, 0, 0.3)];
    }
    return _maskView;
}

-(UIImageView*)topRoketView{
    if(!_topRoketView){
        _topRoketView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"update_progress_rocket"]];
    }
    return _topRoketView;
}

#pragma mark - Properties

-(UIView*)bottomContentView{
    if(!_bottomContentView){
        _bottomContentView = [[UIView alloc]init];
        [_bottomContentView setBackgroundColor:[UIColor whiteColor]];
        [_bottomContentView.layer setCornerRadius:10];
        _bottomContentView.layer.masksToBounds=YES;
        _bottomContentView.clipsToBounds = YES;
    }
    return _bottomContentView;
}

-(UILabel*)alertLabel{
    if(!_alertLabel){
        _alertLabel = [[UILabel alloc]init];
        [_alertLabel setTextAlignment:NSTextAlignmentCenter];
        [_alertLabel setTextColor:RGBA(90, 124, 188, 1)];
        [_alertLabel setFont:[UIFont systemFontOfSize:14]];
    }
    return _alertLabel;
}

-(UIView*)opraView{
    if(!_opraView){
        _opraView = [[UIView alloc]init];
        [_opraView setHidden:YES];
    }
    return _opraView;
}
@end

@interface QHUpdateAlertAndProgress ()

@end

static QHUpdateAlertAndProgress * _currentProgress = nil;

@implementation QHUpdateAlertAndProgress

+ (QHUpdateAlertAndProgress *)currentProgress{
    @synchronized(self)
    {
        if (_currentProgress == nil)
        {
            _currentProgress = [[QHUpdateAlertAndProgress alloc] init];
        }
    }
    return _currentProgress;
}

-(instancetype)init{
    if (self = [super init]) {
        [self setUpUI];
    }
    return self;
}

-(UILabel *)alertLabel{
    if (!_alertLabel) {
        _alertLabel = [[UILabel alloc]init];
        _alertLabel.textColor = [UIColor whiteColor];
        _alertLabel.font = [UIFont systemFontOfSize:15];
        _alertLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _alertLabel;
}
-(UIView*)contentView{
    if(!_contentView){
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, Main_Screen_Width, Main_Screen_Height)];
        _contentView.backgroundColor = [UIColor clearColor];
        [_contentView setAlpha:1];
    }
    return _contentView;
}

-(UIView *)progressView{
    if (!_progressView) {
        _progressView = [[UIView alloc]init];
        _progressView.backgroundColor = RGBA(90, 124, 188, 1);
        _progressView.layer.cornerRadius = 4;
        _progressView.layer.masksToBounds = YES;
    }
    return _progressView;
}

-(UILabel *)progressLab{
    if (!_progressLab) {
        _progressLab = [[UILabel alloc]init];
        _progressLab.font = [UIFont systemFontOfSize:16];
        _progressLab.textColor = [UIColor whiteColor];
    }
    return _progressLab;;
}

-(void)setUpUI
{
    [[AppDelegate sharedAppDelegate].navigationViewController.view  addSubview:self.contentView];
    [self.contentView addSubview:self.alertLabel];
    [self.contentView addSubview:self.progressView];
    [self.contentView addSubview:self.progressLab];
    
    CGFloat y = SCREEN_HEIGHT/2 + 50;

    self.alertLabel.frame = CGRectMake(40, y+30, SCREEN_WIDTH - 80, 20);

    self.progressView.frame = CGRectMake(40, y, 0, 10);
//    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(40);
//        make.top.mas_equalTo(y);
//        make.width.mas_equalTo(0);
//        make.height.mas_equalTo(10);
//    }];
    
    self.progressLab.frame = CGRectMake(SCREEN_WIDTH - 60, y-40, 60, 20);
}

-(void)setProgress:(CGFloat)progress{
    
    if (_isSkipBtnTaped)
    {
        [self HideAllSubviews:YES];
    }
    else
    {
        [self HideAllSubviews:NO];
    }
   
    _progress = progress;
    
    CGFloat width = SCREEN_WIDTH - 80;
    
    self.progressView.frame = CGRectMake(40, SCREEN_HEIGHT/2 + 50, width * progress , 10);
    
//    [self.progressView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(width * progress);
//    }];
    self.alertLabel.text = [NSString stringWithFormat:@"%@",self.TipStr];
    self.progressLab.text = [NSString stringWithFormat:@"%.0f %@",_progress*100,@"%"];
}

-(void)setTipStr:(NSString *)TipStr
{
    if (_isSkipBtnTaped)
    {
        [self HideAllSubviews:YES];
    }
    else
    {
        [self HideAllSubviews:NO];
    }
    
    _TipStr = TipStr;
    self.alertLabel.text = [NSString stringWithFormat:@"%@",TipStr];
}

-(void)HideAllSubviews:(BOOL)isHide
{
    self.alertLabel.hidden = isHide;
    self.progressView.hidden = isHide;
    self.contentView.hidden = isHide;
//    self.skipButton.hidden = isHide;
}

@end
